package org.rency.ues.common;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.Assert;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class UesValidator {

	private final static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

	/**
	 * 注解校验
	 * </br>Created on 2016年7月19日
	 * @param bean
	 */
	public static <T> void validator(T bean){
		List<String> errorList = new ArrayList<String>();
        Set<ConstraintViolation<T>> violations = validator.validate(bean);
        if (!violations.isEmpty()) {
            for (ConstraintViolation<T> violation : violations) {
                errorList.add(violation.getMessage());
            }
        }
        Assert.isTrue(CollectionUtils.isEmpty(errorList), errorList.toString());
	}
	
}